package cn.cdeden.system.dubbo;

import cn.cdeden.system.api.RemoteLgtypeConfigService;
import cn.cdeden.system.api.domain.vo.RemoteConfigVo;
import cn.cdeden.system.domain.SysLgtypeConfig;
import cn.cdeden.system.domain.vo.SysLgtypeConfigVo;
import cn.cdeden.system.mapper.SysLgtypeConfigMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 登录方式配置服务
 *
 * @author Nick
 */
@RequiredArgsConstructor
@Service
@DubboService
public class RemoteLgtypeConfigServiceImpl implements RemoteLgtypeConfigService {

    private final SysLgtypeConfigMapper sysLgtypeConfigMapper;

    @Override
    public Map<String,String> getAllConfigByKeys(String loginType){
        // 查询符合 loginType 的配置列表
        List<SysLgtypeConfigVo> sysLgtypeConfigVos = sysLgtypeConfigMapper.selectVoList(
            new LambdaQueryWrapper<SysLgtypeConfig>().eq(SysLgtypeConfig::getLoginType, loginType)
        );

        // 防止 sysLgtypeConfigVos 为空
        if (sysLgtypeConfigVos == null || sysLgtypeConfigVos.isEmpty()) {
            return Collections.emptyMap();
        }

        // 使用流将配置键值组织成 Map
        return sysLgtypeConfigVos.stream()
            .collect(Collectors.toMap(SysLgtypeConfigVo::getConfigKey, SysLgtypeConfigVo::getConfigValue, (existing, replacement) -> replacement));
    }
}
